//
// Created by cjh on 2021/5/22.
//

#ifndef RESOURCE_ROUTER_H
#define RESOURCE_ROUTER_H
#include <string>
#include <vector>
#include <regex>
#include "view/view.h"


class _route {
public:
    virtual View* match(const char *)const =0;
    virtual ~_route()= default;
};


class url : public _route {
public:
    url(const std::string& pattern, View* view, const std::string& name)
        :pattern(pattern), name(name), view(view) {}

    std::string name;
    std::regex pattern;
    View *view;

    View* match(const char *str)const override;
};


class Router : public _route{
public:
    Router(std::initializer_list<_route*>&& li);
    Router(const std::string& pattern="/"):pattern(pattern) {}
    ~Router() override;
    View* match(const char *str)const;
    std::vector<_route*> urls;
    std::regex pattern;

    Router& operator+=(std::initializer_list<_route*>&& li);
};


inline _route* path(const std::string& pattern, View* view, const std::string& name) {
    return new url(pattern, view, name);
}

inline _route* path(const std::string& pattern, Router& router) {
    auto r = new Router(router);
    r->pattern.assign(pattern);
    return r;
}



extern Router router;


#endif //RESOURCE_ROUTER_H
